#!/bin/sh

set -e

pkgfile=$1
operation=$2

pwrequestfile=/var/etch/requests/etc/passwd/$pkgfile
shrequestfile=/var/etch/requests/etc/shadow/$pkgfile

case "$operation" in
'install')
	# Grab the data passed to us on stdin
	datafile=`mktemp`
	cat > $datafile
	for requestfile in $pwrequestfile $shrequestfile
	do
		mkdir -p `dirname "$requestfile"`
		tmpfile=`mktemp "$requestfile.XXXXXX"` || exit 1
		echo "<request>" > $tmpfile
		# Dump in the data we captured above
		cat $datafile >> $tmpfile
		echo "</request>" >> $tmpfile
		chmod 644 $tmpfile
	done
	rm -f $datafile
	;;
'remove')
	for requestfile in $pwrequestfile $shrequestfile
	do
		rm -f "$requestfile".*
	done
	;;
*)
	echo "$0: Invalid arguments"
	exit 1
	;;
esac

/usr/sbin/etch /etc/passwd /etc/shadow

